package com.keehin.migrate;

import java.sql.SQLException;

import com.keehin.Database;

public class MigrateStock {
	protected static Database srcDB;
	protected static Database descDB;

	public static void main(String[] args) {
		try {
			System.out.println(args[0]);
			System.out.println(args[1]);
			System.out.println(args[2]);
			System.out.println(args[3]);
			srcDB = new Database(new com.mysql.jdbc.Driver(), "jdbc:mysql://"
					+ args[0] + "?characterEncoding=tis620", args[2], args[3]);
			if (srcDB != null)
				System.out.println(srcDB + " OK");
			descDB = new Database(new com.mysql.jdbc.Driver(), "jdbc:mysql://"
					+ args[1] + "?characterEncoding=tis620", args[2], args[3]);
			if (descDB != null)
				System.out.println(descDB + " OK");
		} catch (SQLException e) {
			e.printStackTrace();
		}

		descDB.executeUpdate("delete from prodgroup");
		descDB.executeUpdate("insert into windmill.prodgroup(comCode,proGrp, descript) select '"
				+ args[4] + "',partGroup, Description from migrate.stkgrp");

		descDB.executeUpdate("delete from product");
		descDB.executeUpdate("insert into windmill.product(comCode,proCode, name, proGrp,"
				+ "unit, discount, sellPrice, purPrice, minPrice, purVAT, sellVAT, incomeAcc, costAcc,"
				+ "stockItem, buyItem, sellItem) select '"
				+ args[4]
				+ "',PartIdent, Description, PartGroup, Unit, DefaultDiscount, ListPrice, "
				+ "LastPurchasingPrice, MinSalesPrice, VATCodePurchasing,VATCode, "
				+ " IncomeAccount, CostOfSalesAccount, InventoryItem='Y', SellItem='Y', BuyItem='Y' FROM migrate.stkbody");
		descDB.executeUpdate("update windmill.product set purVAT=0 where purVAT=2");
		descDB.executeUpdate("update windmill.product set sellVAT=0 where sellVAT=2");

		descDB.executeUpdate("delete from windmill.stockloc");
		descDB.executeUpdate("insert into windmill.stockloc select '" + args[4]
				+ "', stockLocation, Description, LedgerAccount, 'admin', CURRENT_TIMESTAMP"
				+ " from migrate.stkloc");

		descDB.executeUpdate("delete from windmill.stock");
		descDB.executeUpdate("insert into windmill.stock select '" + args[4]
				+ "', ItemPartIdent, StockLocation, StockBalance, 0,"
				+ " MinBalance, ReorderQty, 'admin', CURRENT_TIMESTAMP"
				+ " from migrate.stkitem where companyNumber = 1");

	}

	public static void printSymbol(int i) {
		if (i % 500 == 0) {
			System.out.print("#");
			if (i % 100000 == 0)
				System.out.printf("%,10d\n", i);
			else if (i % 50000 == 0)
				System.out.println();
		}
	}
}
